<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: LXListMenuPortlet.php 289 2008-11-14 05:34:40Z steen.rabol@gmail.com $
 * @package Pradoportal.Porttlets.LXListMenuPortlet.Edit
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Common.XListMenu');
Prado::using('Application.Common.Data.PortalConfigRecord');
Prado::using('Application.Portlets.LXListMenuPortlet.Common.LXListmenuRecord');

class LXListMenuPortlet extends Portlet
{
	public $_pRecord;

	public function onLoad($param)
	{
		parent::onLoad($param);
		// Get value from config
		$this->_pRecord = PortalConfigRecord::finder()->findByname('LXListMenu_' . $this->Parameters['id']);

		if($this->_pRecord === null)
		{
			$this->_pRecord = new PortalConfigRecord();
			$this->_pRecord->name	= 'LXListMenu_' . $this->Parameters['id'];
			$this->_pRecord->save();
		}

		if(!$this->Page->IsPostBack)
		{
			$this->ddlAddPages->DataSource	= $this->AvailablePages;
			$this->ddlAddPages->PromptText	= Prado::localize("Select page");
			$this->ddlAddPages->PromptValue	= -1;
			$this->ddlAddPages->dataBind();

			$sql 			= "select * from tbllxlistmenuportlet where id=" . $this->Parameters['id'] . " order by displayorder";
			$this->dlMenuItems->DataSource	= $this->DataAccess->createCommand($sql)->query();
			$this->dlMenuItems->dataBind();


			if($this->_pRecord)
			{
				$css =  explode(";",$this->_pRecord->value);
				$this->tbActiveCss->Text		= (isset($css[0]) ? $css[0] : "");
				$this->tbInActiveCss->Text		= (isset($css[1]) ? $css[1] : "");
				$this->tbMenuCss->Text			= (isset($css[2]) ? $css[2] : "");
				$this->tbMenuTitle->Text		= (isset($css[3]) ? $css[3] : "");
			}
		}
	}

	public function SaveMenuProperties($sender, $param)
	{
		if($this->Page->IsValid)
		{
			// Save the CSS information
			$menucss		= $this->tbMenuCss->Text;
			$activecss		= $this->tbActiveCss->Text;
			$inactivecss	= $this->tbInActiveCss->Text;
			$menutitle		= $this->tbMenuTitle->Text;

			$this->_pRecord->value	= $activecss . ";" . $inactivecss . ";" . $menucss . ";" . $menutitle;
			$this->_pRecord->save();
		}
	}

	public function getAvailablePages()
	{
		return $this->DataAccess->createCommand("select * from tblpages")->query();
	}

	public function AddMenuItem($sender, $param)
	{
		if($this->Page->IsValid)
		{
			$conn				= $this->Application->DbConnection;
			$rec				= new LXListmenuRecord();
			$rec->id 			= TPropertyValue::ensureInteger($this->Parameters['id']);
			$rec->pagename 		= $this->ddlAddPages->SelectedValue;
			$rec->displayname	= $this->tbAddDisplayName->Text;
			$rec->displayorder	= $conn->createCommand("select max(displayorder) from tbllxlistmenuportlet where id=" . $rec->id)->queryScalar();
			$rec->displayorder++;
			$rec->save();
		}

		/*
		$this->ddlAddPages->SelectedIndex = 0;
		$this->tbAddDisplayName->Text	= '';
		$this->dlMenuItems->DataSource	= $this->DataAccess->createCommand("select * from tbllxlistmenuportlet where id=" . $this->Parameters['id'] . " order by displayorder")->query();
		$this->dlMenuItems->dataBind();
		*/
		$this->Page->Response->reload();
	}

	public function deleteItem($sender,$param)
	{
		$id			= $this->Parameters['id'];
		$pagename	= $this->dlMenuItems->DataKeys[$param->Item->ItemIndex];
		LXListmenuRecord::finder()->deleteBy_id_AND_pagename($id,$pagename);

		$this->dlMenuItems->SelectedItemIndex=-1;
		$this->dlMenuItems->EditItemIndex=-1;
		$this->dlMenuItems->DataSource	= $this->DataAccess->createCommand("select * from tbllxlistmenuportlet where id=" . $this->Parameters['id'] . " order by displayorder")->query();
		$this->dlMenuItems->dataBind();
	}

	public function itemCommand($s,$p)
	{
		switch($p->CommandName)
		{
			case "MoveDownLM":	$this->MoveDownLM($p->CommandParameter); break;
			case "MoveUpLM":	$this->MoveUpLM($p->CommandParameter); break;
		}

		$this->dlMenuItems->DataSource	= $this->DataAccess->createCommand("select * from tbllxlistmenuportlet where id=" . $this->Parameters['id'] . " order by displayorder")->query();
		$this->dlMenuItems->dataBind();
	}

	public function MoveUpLM($pagename)
	{
		$row = LXListmenuRecord::finder()->findBy_id_AND_pagename($this->Parameters['id'],$pagename);
		if($row !== null)
		{
			$row->displayorder = $row->displayorder - 1.001;
			$row->save();
			$this->RenumDisplayOrderLM($pagename);
			$this->RenumDisplayOrderLM($pagename,1);
		}
	}

	public function MoveDownLM($pagename)
	{
		$row = LXListmenuRecord::finder()->findBy_id_AND_pagename($this->Parameters['id'],$pagename);
		if($row !== null)
		{
			$row->displayorder = $row->displayorder + 1.001;
			$row->save();
			$this->RenumDisplayOrderLM($pagename);
			$this->RenumDisplayOrderLM($pagename,1);
		}
	}

	public function RenumDisplayOrderLM($pagename,$p_startat = 999.000)
	{
		$newdisplayorder	= $p_startat;

		$criteria=new TActiveRecordCriteria();
		$criteria->OrdersBy['displayorder']='asc';
		$records 			= LXListmenuRecord::finder()->findAll($criteria);

		foreach($records as $record)
		{
			$record->displayorder = $newdisplayorder;
			$record->save();
			$newdisplayorder++;
		}
	}

	public function DeleteButtonClicked($sender,$param)
	{
		PortalConfigRecord::finder()->deleteByname('LXListMenu_' . $this->Parameters['id']);
		parent::DeleteButtonClicked($sender,$param);
	}

}
?>